package com.example.administrator.x3picturetransliteration.Home.Activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.x3picturetransliteration.Base.BaseActivity;
import com.example.administrator.x3picturetransliteration.Base.BasePresenter;
import com.example.administrator.x3picturetransliteration.Bean.CommonProblemBean;
import com.example.administrator.x3picturetransliteration.Home.Adapter.CommonProblemAdapter;
import com.example.administrator.x3picturetransliteration.Home.Presenter.Activity.CommonProblemPresenter;
import com.example.administrator.x3picturetransliteration.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 常见问题
 * Created by Administrator on 2019/8/23.
 */

public class CommonProblemActivity extends BaseActivity implements OnRefreshListener,OnLoadMoreListener {

    @BindView(R.id.TitleText)TextView TitleText;
    @BindView(R.id.LButton)ImageView LButton;

    @BindView(R.id.listview)ListView listview;

    @BindView(R.id.noView)View noView; //无问题情况

    @BindView(R.id.smartRefreshLayout)SmartRefreshLayout smartRefreshLayout; //上拉下拉

    private CommonProblemPresenter mCommonProblemPresenter;

    private List<CommonProblemBean> mData;
    private CommonProblemAdapter mCommonProblemAdapter;

    @Override
    public int intiLayout() {
        return R.layout.home_activity_common_problem;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        TitleText.setText("常见问题");
        LButton.setImageResource(R.mipmap.home_backw);

        mCommonProblemPresenter = new CommonProblemPresenter(this,mCompositeSubscriptions);

        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);

        mData = new ArrayList<>();
        mCommonProblemAdapter = new CommonProblemAdapter(this,mData);

        listview.setAdapter(mCommonProblemAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();

        list(true);
    }

    /** 上拉加载 */
    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {

        list(false);
        refreshLayout.finishLoadMore(2000);
    }

    /** 下拉刷新 */
    @Override
    public void onRefresh(RefreshLayout refreshLayout) {

        list(true);
        refreshLayout.finishRefresh(2000);
    }

    @OnClick(R.id.left_button)
    public void left_buttonClick(){

        finish();
    }

    /** 文档列表 */
    private void list(boolean Refresh){

        if (Refresh){

            mData.clear();
            mCommonProblemAdapter.notifyDataSetChanged();
        }

        mCommonProblemPresenter.list(Refresh, new BasePresenter.Callback() {
            @Override
            public void getData(Object data) {

                List<CommonProblemBean> listdata = (List<CommonProblemBean>)data;

                mData.addAll(listdata);

                noView.setVisibility(mData.size()>0?View.GONE:View.VISIBLE);

                mCommonProblemAdapter.notifyDataSetChanged();

            }
        });

    }

}
